﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SSE.Models;
using System.Xml;
using System.IO;

namespace SSE.Controllers
{
    public class NewsController : Controller
    {
        private void MakeXML()
        {
            var newsToView = (from n in sseEntities.GetInstance().News select n).ToArray().Reverse().ToList();

            XmlDocument xd = new XmlDocument();
            XmlDeclaration xde;
            xde = xd.CreateXmlDeclaration("1.0", "GBK", null);
            xd.AppendChild(xde);

            XmlElement newslist = xd.CreateElement("newslist");
            xd.AppendChild(newslist);
            XmlNode root = xd.SelectSingleNode("newslist");
            XmlElement[] news = new XmlElement[4];
            XmlElement[] text = new XmlElement[4];
            XmlElement[] img = new XmlElement[4];
            XmlElement[] newsId = new XmlElement[4];
            for (int i = 0; i < 4; i++)
            {
                news[i] = xd.CreateElement("news");
                text[i] = xd.CreateElement("text");
                img[i] = xd.CreateElement("img");
                newsId[i] = xd.CreateElement("newsId");
                news[i].SetAttribute("id", i.ToString());
                if (i < newsToView.Count)
                {
                    text[i].InnerText = newsToView[i].Title;
                    img[i].InnerText = newsToView[i].ImageURL;
                    newsId[i].InnerText = newsToView[i].NewsID.ToString();
                }
                else
                {
                    text[i].InnerText = "";
                    img[i].InnerText = "";
                    newsId[i].InnerText = "";
                }
                news[i].AppendChild(text[i]);
                news[i].AppendChild(img[i]);
                news[i].AppendChild(newsId[i]);
                root.AppendChild(news[i]);
            }
            xd.Save(Server.MapPath("../Sources/News.xml"));
        }

        private string getPic(string path, string newsName)
        {
            string picName = "News_" + newsName;
            string saveUrl = "../Sources/NewsPic/";
            string dirPath = Server.MapPath(saveUrl);

            HttpPostedFileBase newsPic = Request.Files["newsPic"];
            string fileExt = Path.GetExtension(newsPic.FileName).ToLower();
            newsPic.SaveAs(dirPath + "News_" + newsName + fileExt);
            return "Sources/NewsPic/" + picName + fileExt;
        }
        //列出所有新闻
        public ActionResult Index()
        {
            var newsToView = (from n in sseEntities.GetInstance().News select n).ToArray().Reverse();
            ViewData.Model = newsToView;
            return View();
        }
        //查看新闻
        public ActionResult ViewNews(int id)
        {
            var newsToView = sseEntities.GetInstance().News.First(m => m.NewsID == id);
            ViewData["news"] = newsToView;
            return View();
        }
        //编辑新闻
        [ValidateInput(false)]
        public ActionResult EditNews()
        {
            if (LoginController.WrongPWD)
            {
                return RedirectToAction("Alert", "Login");
            }
            return View();
        }
        //创建新闻
        //        public ActionResult AddNews()
        //        {
        //            return View();
        //        }
        //        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult AddNews(FormCollection form)
        {
            if (LoginController.WrongPWD)
            {
                return RedirectToAction("Alert", "Login");
            }

            var newsToAdd = new News();
            newsToAdd.UserID = LoginController.adminID;
            newsToAdd.AddedDate = DateTime.Now;
            newsToAdd.AddedBy = LoginController.adminName;
            newsToAdd.Body = form["ctl00$cpMainContent$body"];
            newsToAdd.Title = form["title"];
            newsToAdd.Author = LoginController.adminName;
            newsToAdd.Deleted = false;
            newsToAdd.Listed = true;
            newsToAdd.ImageURL = getPic(form["newsPic"], newsToAdd.Title);
            sseEntities.GetInstance().AddToNews(newsToAdd);
            sseEntities.GetInstance().SaveChanges();
            ViewData["news"] = newsToAdd;

            MakeXML();
            return View("ViewNews");
        }
        //删除新闻
        public ActionResult RemoveNews(int id)
        {
            if (LoginController.WrongPWD)
            {
                return RedirectToAction("Alert", "Login");
            }
            var newsToDelete = sseEntities.GetInstance().News.First(m => m.NewsID == id);
            sseEntities.GetInstance().DeleteObject(newsToDelete);
            sseEntities.GetInstance().SaveChanges();
            return View();
        }

        public ActionResult ManageNews()
        {
            if (LoginController.WrongPWD)
            {
                return RedirectToAction("Alert", "Login");
            }
            var newsToView = (from n in sseEntities.GetInstance().News select n).ToArray().Reverse();
            ViewData.Model = newsToView;
            return View();
        }
    }
}
